﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Chefbook.HelperMethods;
using NUnit.Framework;
using System.Collections.ObjectModel;
using System.Collections.Specialized;

namespace UnitTests.HelperMethodsTests
{
    [TestFixture]
    public class CollectionExtensionMethodsFixture
    {
        [Test]
        public void AddIfNewTest1()
        {
            List<object> list1 = new List<object>();
            object a = new object();
            list1.AddIfNewAndNotNull(a);
            Assert.AreEqual(1, list1.Count((item) => item == a),"Object did not appear in collection exactly once");
        }
        [Test]
        public void AddIfNewTest2()
        {
            List<object> list1 = new List<object>();
            object a = new object();
            list1.AddIfNewAndNotNull(a);
            list1.AddIfNewAndNotNull(a);
            Assert.AreEqual(1, list1.Count((item) => item == a), "Object did not appear in collection exactly once");
        }

        [Test]
        public void AddAndThrowTest1()
        {
            List<int> ints = new List<int>();
            ints.Add(3);
            Assert.Throws<ArgumentException>(() => ints.AddAndThrowIfNullOrNotNew(3));
        }

        [Test]
        public void AddAndThrowTest2()
        {
            List<string> strings = new List<string>();
            Assert.Throws<ArgumentNullException>(() => strings.AddAndThrowIfNullOrNotNew(null));
        }

        [Test]
        public void ModifyCollectionToMatchNewCollectionTest1()
        {
            ObservableCollection<int> testList = new ObservableCollection<int>(new int[] { 1, 2, 3, 4 });
            List<int> newlist = new List<int>(new int[] { 2, 5, 4 });
            var args = testList.AssertCollectionChangedFiredNTimesAndReturnArgs(() => testList.ModifyCollectionToMatchNewCollection(newlist));
            Assert.True(args.Contains(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, 5), ChangedEventHelper.NotifyCollectionChangedEventArgsEqual));
            Assert.True(args.Contains(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, 1), ChangedEventHelper.NotifyCollectionChangedEventArgsEqual));
            Assert.True(args.Contains(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, 3), ChangedEventHelper.NotifyCollectionChangedEventArgsEqual));
        }

        [Test]
        public void HasSameEntriesAsTest()
        {
            List<int> list1 = new List<int>(new int[] { 2, 5, 4 });
            List<int> list2 = new List<int>(new int[] { 2, 4, 5 });
            List<int> list3 = new List<int>(new int[] { 1, 4, 5 });
            Assert.True(list1.HasSameEntriesAs(list2));
            Assert.True(list2.HasSameEntriesAs(list1));
            Assert.False(list2.HasSameEntriesAs(list3));
            Assert.False(list3.HasSameEntriesAs(list2));
            Assert.False(list1.HasSameEntriesAs(list3));
            Assert.False(list3.HasSameEntriesAs(list1));
        }
    }
}
